package com.dk.controller;

import com.alibaba.fastjson.JSONObject;
import com.dk.VO.TbOrderVO;
import com.dk.VO.TbUserVO;
import com.dk.bo.AlipayOrderBO;
import com.dk.enums.ResultEnum;
import com.dk.result.Result;
import com.dk.service.TbOrderService;
import com.dk.util.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("order")
@Api
@ApiOperation("订单")
public class TbOrderController {

    @Autowired
    private TbOrderService orderService;
    @Autowired
    private JwtUtil jwtUtil;

    @GetMapping("getOrdersByUserId")
    @ApiOperation(value = "查询订单")
    @ApiImplicitParam(name = "AuthToken", value = "登录令牌", required = true, type = "path")
    public Result getOrdersByUserId(@RequestHeader("AuthToken") String token) {
        Claims claims = jwtUtil.parseJwt(token);
        TbOrderVO orderVO = JSONObject.parseObject(claims.getSubject(), TbOrderVO.class);
        try {
            List<TbOrderVO> orderList = orderService.getOrdersByUserId(orderVO.getUserId());
            return Result.OK().setData(orderList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }

    @GetMapping("getSoldCars")
    @ApiOperation(value = "查询已卖出订单")
    @ApiImplicitParam(name = "AuthToken", value = "登录令牌", required = true, type = "path")
    public Result getSoldCars(@RequestHeader("AuthToken") String token) {
        Claims claims = jwtUtil.parseJwt(token);
        TbOrderVO tbOrderVO = JSONObject.parseObject(claims.getSubject(), TbOrderVO.class);
        try {
            List<TbOrderVO> orderList = orderService.getSoldCars(tbOrderVO.getUserId());
            return Result.OK().setData(orderList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }

    @PostMapping("pay")
    @ApiOperation(value = "生成订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cid", value = "车辆编号", required = true, type = "path"),
            @ApiImplicitParam(name = "AuthToken", value = "登录令牌", required = true, type = "path"),
            @ApiImplicitParam(name = "num", value = "付款金额", required = true, type = "path")
    })
    public Result pay(@RequestParam Long cid, @RequestHeader("AuthToken") String AuthToken,@RequestParam Long num) {
        try {
            //获取登录用户
            Claims claims = jwtUtil.parseJwt(AuthToken);
            TbUserVO userVO = JSONObject.parseObject(claims.getSubject(), TbUserVO.class);
            if (userVO == null) {
                return Result.respones(ResultEnum.LOGIN_AUTH);
            }
            //创建订单
            AlipayOrderBO orderBO = orderService.createOrder(userVO.getUserid(), userVO.getPhone(), cid, num);
            return Result.OK().setData(orderBO);
        } catch (Exception e) {
            return Result.ER();
        }
    }
}
